%calcMW will calculate the molecular weight of an amino acid sequence in
%g/mol. 
%
%  MW = calcMW(Seq)
%
%  INPUT
%    Seq: string or cell of string of amino acid sequences
%         *If empty, will return the Info structure
%
%  OUTPUT
%    MW: value or matrix of MW values
%    Info: cell of MW data (without water weight of 18.015)
%
%  REFERENCE
%  Cantor and Schimmel 1980.
%  http://www.proteinsandproteomics.org/content/free/tables_1/table08.pdf

function MW = calcMW(Seq)
Info = {...
    'A',  71.04;...
    'R', 156.19; ...
    'N', 114.10; ...
    'D', 115.09; ...
    'C', 103.14; ...
    'Q', 128.13; ...
    'E', 129.12; ...
    'G',  57.05; ...
    'H', 137.14; ...
    'I', 113.16; ...
    'L', 113.16; ...
    'K', 128.17; ...
    'M', 131.20; ...
    'F', 147.18; ...
    'P',  97.12; ...
    'S',  87.08; ...
    'T', 101.11; ...
    'W', 186.21; ...
    'Y', 163.18; ...
    'V', 99.13,; ...
    }; %Mass WITHOUT water weight. Must add 1 water weight at end (18.015);
if nargin == 0
    MW = Info;
    return
end

AAMW = cell2mat(Info(:, 2)); %amino acid mw
Water = 18.015;

if iscell(Seq)
    MW = zeros(size(Seq));
    for j = 1:numel(Seq)
        MW(j) = sum(AAMW .* countAA(Seq{j}, Info)) + Water;
    end
else
    MW = sum(AAMW .* countAA(Seq, Info)) + Water;
end

function C = countAA(Seq, Info)
Seq = upper(Seq);
C = cellfun(@(x) sum(Seq == x), Info(:, 1));